package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.Service.LogService;
import com.shop.Service.UserService;
import com.shop.Utils.TokenUtils;
import com.shop.common.TokenService;
import com.shop.pojo.Message;
import com.shop.pojo.User;
import com.shop.pojo.UserImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService ;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private LogService logService;

    /**
     * @Author 李皓天
     * 登录模块
     * @return json
     * @throws Exception
     */
    @PostMapping(value = "/login")
    @ResponseBody
    public Message login(@RequestBody Map<String,Object> map) throws Exception {
        String username = map.get("username").toString();
        String password = map.get("password").toString();
        User user = userService.login(username,password);
        Message<String> message = new Message<String>();
        if (user !=null) {
            message.setStatus("200");
            message.setMessage("获取成功");
            String token= TokenUtils.token(user.getUsername(),user.getPassword());
            message.setData(token);

        }else {
            message.setStatus("200");
            message.setMessage("密码用户名错误");

        }
        return message;
    }
    @PostMapping(value = "/loginNew")
    @ResponseBody
    public Message loginNew(@RequestBody Map<String,Object> map) throws Exception {
        String username = map.get("username").toString();
        String password = map.get("password").toString();
        User user = userService.login(username,password);
        Message<String> message = new Message<String>();
        if (user !=null) {
            message.setStatus("200");
            message.setMessage("获取成功");
            String token= TokenUtils.token(user.getUsername(),user.getPassword());
            message.setData(token);
            logService.addLog(username,true,"登录");
        }else {
            message.setStatus("200");
            message.setMessage("密码用户名错误");
            logService.addLog(username,false,"登录");
        }
        return message;
    }

    /**
     * @Author 李皓天
     * 需要验证Authorization
     * @param Authorization
     * @return
     * @throws Exception
     */
    @PostMapping("/addUser")
    @ResponseBody
    public Message<String> addUser(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<String>();
        String username = map.get("username").toString();
        String password = map.get("password").toString();
        message = userService.TokenYan(Authorization);
        if (message.getStatus().equals("401")){
            return message;
        }
        if (!tokenService.getUsername(Authorization).equals("root")){
            message.setStatus("201");
            message.setMessage("非超级管理员");
            return message;
        }
        if (message.getData()==null){
            message = userService.addUser(username,password);
            if(message.getData() ==null){
                logService.addLog(tokenService.getUsername(Authorization),false,username+"的创建");
            }else {
                logService.addLog(tokenService.getUsername(Authorization),true,username+"的创建");
            }

        }
        message.setStatus("201");
        return message;
    }

    /**
     * @Author 李皓天
     * 获得查询用户
     * @return
     * @throws Exception
     */
    @PostMapping("/selectAllUser")
    @ResponseBody
    public Message<List<User>> selectAllUser(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<List<User>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String queryInfo = ""+map.get("queryInfo");
        int pageSize =  Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        message = userService.selectAllUser(queryInfo, pageSize, pageNum);
        message.setStatus("200");
        if (message.getData()==null){
            message.setMessage("没有查找到相关内容");
            return message;
        }
        message.setMessage("获取成功");
        return message;
    }

    /**
     *
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/selectAllUserPage")
    @ResponseBody
    public Message<PageInfo<User>> Page(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<PageInfo<User>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String queryInfo = map.get("queryInfo").toString();
        int pageSize =  Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        message = userService.selectAllUserPage(queryInfo, pageSize, pageNum);
        message.setStatus("200");
        if (message.getData()==null){
            message.setMessage("没有查找到相关内容");
            return message;
        }
        message.setMessage("获取成功");
        return message;
    }
    @PostMapping("/updatePassword")
    @ResponseBody
    public Message<String> updatePassword(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String username = map.get("username").toString();
        String password = map.get("password").toString();
        message = userService.TokenYan(Authorization);
        if (message.getStatus().equals("401")){
            return message;
        }
        if (!tokenService.getUsername(Authorization).equals("root")){
            message.setStatus("201");
            message.setMessage("非超级管理员");
            return message;
        }
        String oldAdmin = TokenUtils.JieMa(Authorization)[0];
        message = userService.updatePassword(username,password);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,username+"的密码修改");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的密码修改");
        }
        message.setStatus("200");
        if (username.equals(oldAdmin)){
            message.setStatus("401");
            message.setMessage("你修改的是当前用户请重新登录");
            message.setData("请重新登录");
        }

        return message;
    }
    @PostMapping("/updatePasswordTwo")
    @ResponseBody
    public Message<String> updatePassword2(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String username = tokenService.getUsername(Authorization);
        String password = map.get("password").toString();
        message = userService.TokenYan(Authorization);
        if (message.getStatus().equals("401")){
            return message;
        }
        String oldAdmin = TokenUtils.JieMa(Authorization)[0];
        message = userService.updatePassword(username,password);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,username+"的密码修改");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的密码修改");
        }
        message.setStatus("200");
        if (username.equals(oldAdmin)){
            message.setStatus("401");
            message.setMessage("你修改的是当前用户请重新登录");
            message.setData("请重新登录");
        }
        return message;
    }

    /**
     *
     * @param Authorization
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/deleteUser")
    @ResponseBody
    public Message<String> delete(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        if (!tokenService.getUsername(Authorization).equals("root")){
            message.setStatus("201");
            message.setMessage("非超级管理员");
            return message;
        }
        String username = map.get("username").toString();
        if (username.equals("root")){
            message.setStatus("201");
            message.setMessage("root用户不能删除");
            return message;
        }
        userService.deleteImg(username);
        message=userService.deleteUser(username);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,username+"的删除");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的删除");
        }
        message.setStatus("201");
        return message;
    }

    /**
     *
     * @param Authorization
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/addImg")
    @ResponseBody
    public Message<String> addImg(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String username = map.get("username").toString();
        String uimgurl = map.get("uimgurl").toString();
        UserImg userImg = new UserImg(username,uimgurl);
        message=userService.addImg(userImg);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的头像添加");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的头像添加");
        }
        message.setStatus("201");
        return message;
    }

    /**
     *
     * @param Authorization
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/updateImg")
    @ResponseBody
    public Message<String> updateImg(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String username = map.get("username").toString();
        String uimgurl = map.get("username").toString();
        UserImg userImg = new UserImg(username,uimgurl);
        message=userService.updateImg(userImg);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,username+"的头像更新");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的头像更新");
        }
        message.setStatus("201");
        return message;
    }
    @RequestMapping("/deleteImg")
    @ResponseBody
    public Message<String> deleteImg(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String username = tokenService.getUsername(Authorization);
        message=userService.deleteImg(username);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,username+"的图片删除");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,username+"的图片删除");
        }
        message.setStatus("201");
        return message;
    }
    @RequestMapping("/selectImg")
    @ResponseBody
    public Message<String> selectImg(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String username = tokenService.getUsername(Authorization);
        message=userService.selectImg(username);
        if (username.equals("root")){
            message.setMessage("超级管理员");
        }else {
            message.setMessage("管理员");
        }
        message.setStatus("200");
        return message;
    }
    @PostMapping("/selectImgLogin")
    @ResponseBody
    public Message<String> selectImg2(@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        String username = map.get("username").toString();
        message=userService.selectImg(username);
        message.setStatus("200");
        return message;
    }
    @CrossOrigin
    @RequestMapping(value = "/uploadUser",method = RequestMethod.POST)
    @ResponseBody
    public Message<String> uploaduser(@RequestHeader(value = "Authorization",required = true) String Authorization,MultipartFile uploadFile) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
//        System.out.println(uploadFile);
        String originalFilename = uploadFile.getOriginalFilename();
//        System.out.println(originalFilename);
        uploadFile.transferTo(new File("C:\\Users\\mashi\\Desktop\\ssm_vue_shop\\src\\assets\\img\\user\\"+originalFilename));
        UserImg userImg = new UserImg(tokenService.getUsername(Authorization),originalFilename);
        message = userService.addImg(userImg);
        message.setStatus("201");
        if (message.getData()!=null){
            logService.addLog(tokenService.getUsername(Authorization),false,tokenService.getUsername(Authorization)+"的头像上传");
        }
        logService.addLog(tokenService.getUsername(Authorization),true,tokenService.getUsername(Authorization)+"的头像上传");
        message.setData(originalFilename);
        return message;
    }
    @RequestMapping("/selectCount")
    @ResponseBody
    public Message<Integer> selectCount(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception{
        Message<Integer> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        message = userService.selectCount();
        message.setStatus("200");
        return message;
    }





}
